<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Separating ellipsoids in 2D</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/separate_ell_2D.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Separating ellipsoids in 2D</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Joelle Skaf - 11/06/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% Finds a separating hyperplane between 2 ellipsoids {x| ||Ax+b||^2&lt;=1} and</span>
<span class="comment">% {y | ||Cy + d||^2 &lt;=1} by solving the following problem and using its</span>
<span class="comment">% dual variables:</span>
<span class="comment">%               minimize    ||w||</span>
<span class="comment">%                   s.t.    ||Ax + b||^2 &lt;= 1       : lambda</span>
<span class="comment">%                           ||Cy + d||^2 &lt;= 1       : mu</span>
<span class="comment">%                           x - y == w              : z</span>
<span class="comment">% the vector z will define a separating hyperplane because z'*(x-y)&gt;0</span>

<span class="comment">% input data</span>
n = 2;
A = eye(n);
b = zeros(n,1);
C = [2 1; -.5 1];
d = [-3; -3];

<span class="comment">% solving for the minimum distance between the 2 ellipsoids and finding</span>
<span class="comment">% the dual variables</span>
cvx_begin
    variables <span class="string">x(n)</span> <span class="string">y(n)</span> <span class="string">w(n)</span>
    dual <span class="string">variables</span> <span class="string">lam</span> <span class="string">muu</span> <span class="string">z</span>
    minimize ( norm(w,2) )
    subject <span class="string">to</span>
    lam:    square_pos( norm (A*x + b) ) &lt;= 1;
    muu:    square_pos( norm (C*y + d) ) &lt;= 1;
    z:      x - y == w;
cvx_end


t = (x + y)/2;
p=z;
p(1) = z(2); p(2) = -z(1);
c = linspace(-2,2,100);
q = repmat(t,1,length(c)) +p*c;

<span class="comment">% figure</span>
nopts = 1000;
angles = linspace(0,2*pi,nopts);
[u,v] = meshgrid([-2:0.01:4]);
z1 = (A(1,1)*u + A(1,2)*v + b(1)).^2 + (A(2,1)*u + A(2,2)*v + b(2)).^2;
z2 = (C(1,1)*u + C(1,2)*v + d(1)).^2 + (C(2,1)*u + C(2,2)*v + d(2)).^2;
contour(u,v,z1,[1 1]);
hold <span class="string">on</span>;
contour(u,v,z2,[1 1]);
axis <span class="string">square</span>
plot(x(1),x(2),<span class="string">'r+'</span>);
plot(y(1),y(2),<span class="string">'b+'</span>);
line([x(1) y(1)],[x(2) y(2)]);
plot(q(1,:),q(2,:),<span class="string">'k'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling sedumi: 21 variables, 8 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 8, order n = 17, dim = 24, blocks = 6
nnz(A) = 24 + 0, nnz(ADA) = 30, nnz(L) = 19
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            4.00E+00 0.000
  1 :  -5.32E-02 1.36E+00 0.000 0.3414 0.9000 0.9000   2.28  1  1  2.5E+00
  2 :   1.06E+00 3.17E-01 0.000 0.2324 0.9000 0.9000   1.18  1  1  5.3E-01
  3 :   1.19E+00 6.24E-02 0.000 0.1968 0.9000 0.9000   1.23  1  1  9.4E-02
  4 :   1.19E+00 4.54E-04 0.000 0.0073 0.9990 0.9990   1.02  1  1  6.7E-04
  5 :   1.19E+00 1.91E-05 0.000 0.0420 0.9900 0.9900   1.00  1  1  2.8E-05
  6 :   1.19E+00 5.96E-07 0.000 0.0313 0.9901 0.9900   1.00  1  1  8.1E-07
  7 :   1.19E+00 2.48E-08 0.466 0.0416 0.9900 0.9905   1.00  1  1  3.9E-08
  8 :   1.19E+00 4.88E-09 0.000 0.1967 0.9009 0.9000   1.00  2  2  7.3E-09

iter seconds digits       c*x               b*y
  8      0.1   8.3  1.1924413559e+00  1.1924413499e+00
|Ax-b| =   1.3e-08, [Ay-c]_+ =   0.0E+00, |x|=  3.9e+00, |y|=  1.7e+00

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    1.000E-01    2.000E-02    
Max-norms: ||b||=3, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.31165.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.19244
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="separate_ell_2D__01.png" alt=""> 
</div>
</div>
</body>
</html>